package cc.mrbird.unit.controller;

import cc.mrbird.common.annotation.Log;
import cc.mrbird.common.controller.BaseController;
import cc.mrbird.common.domain.QueryRequest;
import cc.mrbird.common.domain.ResponseBo;
import cc.mrbird.common.util.FileUtil;
import cc.mrbird.grid.domain.GridInfo;
import cc.mrbird.system.domain.User;
import cc.mrbird.unit.domain.UnitInfo;
import cc.mrbird.unit.domain.UnitType;
import cc.mrbird.unit.service.UnitInfoService;
import cc.mrbird.unit.service.UnitTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Api(description = "单位管理基本信息接口")
@RestController
public class UnitInfoController  extends BaseController {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private UnitInfoService unitInfoService;

    @Log("获取单位基本信息")
    @RequestMapping(value = "unitinfo",method = RequestMethod.POST)
    /* @RequiresPermissions("unitinfo:list")*/
    public String index() {
        return "unit/unitinfo/unitinfo";
    }

    @Log("获取单位基本信息")
    @ApiOperation(value = "获取单位基本信息" ,  notes="获取单位基本信息")
    @RequestMapping(value = "unitinfo/list",method = RequestMethod.POST)
    /*@RequiresPermissions("unitinfo:list")*/
    @ResponseBody
    public Map<String, Object> unitInfoList(QueryRequest request, UnitInfo unitInfo) {
        return super.selectByPageNumSize(request, () -> this.unitInfoService.findUnitInfoWithType(unitInfo, request));
    }

    @ApiOperation(value = "根据id查询单位信息" ,  notes="根据id查询单位信息")
    @RequestMapping(value = "unitinfo/getUnitInfo",method = RequestMethod.GET)
    @ResponseBody
    public ResponseBo getUnitInfo(Long unit_id) {
        try {
            UnitInfo unitInfo = unitInfoService.findById(unit_id);
            return ResponseBo.ok(unitInfo);
        } catch (Exception e) {
            log.error("获取单位信息失败", e);
            return ResponseBo.error("获取单位失败，请联系网站管理员！");
        }
    }
    @ApiOperation(value = "导出Excel")
    @RequestMapping(value = "unitinfo/excel",method = RequestMethod.POST)
    @ResponseBody
    public ResponseBo managementExcel(UnitInfo unitInfo){
        try {
            List<UnitInfo> list = this.unitInfoService.findUnitInfoWithType(unitInfo, null);
            return FileUtil.createExcelByPOIKit("单位基本信息表", list, UnitInfo.class);
        } catch (Exception e) {
            log.error("导出单位基本信息表Excel失败", e);
            return ResponseBo.error("导出Excel失败，请联系网站管理员！");
        }
    }

    @ApiOperation(value = "导出csv")
    @RequestMapping(value = "unitinfo/csv",method = RequestMethod.POST)
    @ResponseBody
    public ResponseBo managementCsv(UnitInfo unitInfo) {
        try {
            List<UnitInfo> list = this.unitInfoService.findUnitInfoWithType(unitInfo, null);
            return FileUtil.createCsv("单位基本信息表", list, UnitInfo.class);
        } catch (Exception e) {
            log.error("导出单位基本信息表Csv失败", e);
            return ResponseBo.error("导出Csv失败，请联系网站管理员！");
        }
    }
    @ApiOperation(value = "导入excel")
    @RequestMapping(value = "unitinfo/import",method = RequestMethod.POST)
    public ResponseBo eximport(@RequestParam("file") MultipartFile file) {
        boolean a = false;
        String fileName = file.getOriginalFilename();
        try {
            a = unitInfoService.batchImport(fileName, file);
            return  ResponseBo.ok(a);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  ResponseBo.error("导入数据失败,请重新检查excel数据导入");
    }

    @ApiOperation(value = "单位基本添加")
    @RequestMapping(value = "unitinfo/add",method = RequestMethod.POST)
    @ResponseBody
    public ResponseBo add(UnitInfo unitInfo) {
        try {
            UnitInfo result = this.unitInfoService.findByName(unitInfo.getUnit_name());
            if (result != null) {
                return ResponseBo.warn("该单位基本已存在！");
            }
            User user = super.getCurrentUser();
            this.unitInfoService.add(unitInfo,user);
            return ResponseBo.ok();
        } catch (Exception e) {
            log.error("添加失败", e);
            return ResponseBo.error("添加失败，请重新添加！");
        }
    }
    @ApiOperation(value = "修改单位基本信息")
    @Log("修改单位基本信息")
    /* @RequiresPermissions("unitinfo:update")*/
    @RequestMapping(value = "unitinfo/update" ,method = RequestMethod.POST)
    @ResponseBody
    public ResponseBo update(UnitInfo unitInfo) {
        try {
            this.unitInfoService.update(unitInfo);
            return ResponseBo.ok("修改单位基本信息成功！");
        } catch (Exception e) {
            log.error("修改单位基本信息失败", e);
            return ResponseBo.error("修改单位基本信息失败，请联系网站管理员！");
        }
    }
    @Log("删除单位基本信息")
    /*@RequiresPermissions("unitinfo:delete")*/
    @RequestMapping(value = "unitinfo/delete",method = RequestMethod.GET)
    @ResponseBody
    public ResponseBo delete(String ids) {
        try {
            this.unitInfoService.delete(ids);
            return ResponseBo.ok("删除单位基本信息成功！");
        } catch (Exception e) {
            log.error("删除单位基本信息失败", e);
            return ResponseBo.error("删除单位基本信息失败，请联系网站管理员！");
        }
    }
}
